// pages/points/points.js
const app = getApp();
var config = app.globalData.config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowTips: true,
    pageIndex: 1,
    pageSize: 20,
    points: [],
    userInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.getSUserInfo();
    var that = this;
    var user = wx.getStorageSync('sUserInfo');
    that.setData({
      userInfo: user
    })
    this.getPoints();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading();
    this.setData({
      pageIndex: 1,
      points: []
    })
    this.getPoints();
    wx.hideNavigationBarLoading();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.isShowTips) {
      return;
    }
    var page = this.data.pageIndex + 1;
    this.setData({
      pageIndex: page
    })
    this.getPoints();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getPoints() {
    var that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: config.pointsListUrl,
      method: "GET",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        pageIndex: that.data.pageIndex,
        pageSize: that.data.pageSize,
        userId: that.data.userInfo.id
      },
      success: function(res) {
        if (res.data.result == 1) {
          // 回调函数
          var moment_list = that.data.points;
          for (var i = 0; i < res.data.data.length; i++) {
            moment_list.push(res.data.data[i]);
          }
          // 设置数据
          that.setData({
            points: moment_list,
            isShowTips: res.data.data.length == 0 || res.data.data.length < that.data.pageSize,
          })
        }
      },
      fail(res) {},
      complete(res) {
        wx.hideLoading();
      }
    })
  },
  onCheckIn() {
    var that = this;
    wx.request({
      url: config.addPointsUrl,
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        userId: this.data.userInfo.id,
        pointsRule: "REGISTRATION"
      },
      success: function(resp) {
        if (resp.data.result == 1) {
          wx.showToast({
            title: "签到成功",
            icon: 'none'
          })
          that.onLoad()
        } else {
          wx.showToast({
            title: resp.data.msg,
            icon: 'none'
          })
        }
      },
      fail: function(e) {
        wx.showToast({
          title: '签到失败，请重试！',
          icon: 'none'
        })
      },
      complete: function() {}
    })
  },
  onRule() {
    wx.navigateTo({
      url: '/pages/rule/rule',
    })
  }
})